package com.hnhy.hnhyscms_ws.controller;

import com.hnhy.hnhyscms_ws.entity.Cpinsto;
import com.hnhy.hnhyscms_ws.entity.Cpstorage;
import com.hnhy.hnhyscms_ws.service.ICpinstoService;
import com.hnhy.hnhyscms_ws.service.ICpstorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/cpinsto")
public class CpinstoController {
    @Autowired
    private ICpinstoService iCpinstoService;
    @Autowired
    private ICpstorageService iCpstorageService;
    @RequestMapping(value = "/addCpinsto",method= RequestMethod.POST)
    public synchronized void addCpinsto(@RequestParam String productNo,@RequestParam Float amount,@RequestParam String thirdOrderNo){
        Cpstorage cpstorage=iCpstorageService.getByProductNo(productNo);
        Cpinsto cpinsto=new Cpinsto();
        cpinsto.setAmount(amount);
        cpinsto.setBand(cpstorage.getBand());
        cpinsto.setPrice(cpstorage.getPrice());
        cpinsto.setProductName(cpstorage.getProductName());
        cpinsto.setProductNo(productNo);
        cpinsto.setSpec(cpstorage.getSpec());
        cpinsto.setThirdOrderNo(thirdOrderNo);
        cpinsto.setUnit(cpstorage.getUnit());
        cpinsto.setTotal(amount*cpstorage.getPrice());
        iCpinstoService.addCpinsto(cpinsto);
        cpstorage.setAmount(amount+cpstorage.getAmount());
        iCpstorageService.updateByProductNo(cpstorage);
    }
}
